﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WcfCloudMusic.DataContracts;

namespace WcfCloudMusic.DataLayer
{
    /// <summary>
    /// CloudMusicDataLayer
    /// </summary>
    public class CloudMusicDataLayer : ICloudMusicDataLayer
    {

        /// <summary>
        /// Logins the specified user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>
        /// UserId
        /// </returns>
        public int? Login(DCUser user)
        {
            using (CloudMusicDataContext context = new CloudMusicDataContext())
            {
                int? id = null;
                context.GetUserIdByUsernameAndPassword(ref id, user.Username, user.Password);
                return id;
            }
        }

        public bool Upload(DCUploadRequest data)
        {
            using (CloudMusicDataContext context = new CloudMusicDataContext())
            {
                try
                {
                    context.Connection.Open();
                    context.Transaction = context.Connection.BeginTransaction();

                    data.Gps.UserId = data.UserId;
                    int? gpsId = AddGps(context, data.Gps);
                    if (!gpsId.HasValue)
                    {
                        context.Transaction.Rollback();
                        return false;
                    }

                    data.Trafic.GpsId = gpsId.Value;
                    int? traficId = AddTrafic(context, data.Trafic);
                    if (!traficId.HasValue)
                    {
                        context.Transaction.Rollback();
                        return false;
                    }

                    data.Weather.GpsId = gpsId.Value;
                    int? weatherId = AddWeather(context, data.Weather);
                    if (!weatherId.HasValue)
                    {
                        context.Transaction.Rollback();
                        return false;
                    }

                    data.Playlist.UserId = data.UserId;
                    int? playlistId = AddPlaylist(context, data.Playlist);
                    if (!playlistId.HasValue)
                    {
                        context.Transaction.Rollback();
                        return false;
                    }

                    data.Music.GpsId = gpsId.Value;
                    data.Music.PlaylistId = playlistId.Value;
                    data.Music.UserId = data.UserId;
                    int? musicId = AddMusic(context, data.Music);
                    if (!musicId.HasValue)
                    {
                        context.Transaction.Rollback();
                        return false;
                    }

                    context.Transaction.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    context.Transaction.Rollback();
                    return false;
                }
            }
        }


        private int? AddGps(CloudMusicDataContext context, DCGps gps)
        {
            int? id = null;
            context.GPSAddGPSData(ref id, gps.Street, gps.City, gps.Country, gps.Lat, gps.Long, gps.UserId);
            return id;
        }

        private int? AddTrafic(CloudMusicDataContext context, DCTrafic trafic)
        {
            int? id = null;
            context.TraficAddTraficData(ref id, trafic.Status, trafic.GpsId);
            return id;
        }

        private int? AddWeather(CloudMusicDataContext context, DCWeather weather)
        {
            int? id = null;
            context.WeatherAddWeatherData(ref id, weather.Status, weather.DegreeCelsius, weather.GpsId);
            return id;
        }

        private int? AddPlaylist(CloudMusicDataContext context, DCPlaylist playlist)
        {
            int? id = null;
            DateTime? dateCreated = null;
            DateTime? lastPlayed = null;
            DateTime date;
            if (DateTime.TryParse(playlist.DateCreated, out date))
            {
                dateCreated = date;
            }

            if (DateTime.TryParse(playlist.LastPlayed, out date))
            {
                lastPlayed = date;
            }
          
            context.PlaylistAddPlaylistData(ref id, playlist.Name, dateCreated, lastPlayed, playlist.UserId);
            return id;
        }

        private int? AddMusic(CloudMusicDataContext context, DCMusic music)
        {
            int? id = null;
            DateTime? dateCreated = null;
            DateTime? lastPlayed = null;
            DateTime date;
            if (DateTime.TryParse(music.DateAdded, out date))
            {
                dateCreated = date;
            }

            if (DateTime.TryParse(music.LastPlayed, out date))
            {
                lastPlayed = date;
            }

            context.MusicAddMusicData(ref id, music.Name, music.Artist, music.AlbumArtist, music.Album, music.Year, music.Genre,
                music.Format, music.FileSize, music.TrackNo, dateCreated, music.BitRate, music.SampleRate, music.TotalTime,
                music.FileLocation, lastPlayed, music.PlaylistId, music.GpsId, music.UserId);
            return id;
        }
    }
}